const { ccclass, property } = cc._decorator;
import ProgressBar from '../ProgressBar';
import loadGame from './loadGame';
import MyEnums from '../MyEnums';

@ccclass
export default class gameRoot extends cc.Component {
    @property(cc.Node)
    bgNode: cc.Node = null;
    @property(cc.Node)
    barNode: cc.Node = null;

    _barControl: ProgressBar = null;
    _gameBundle: cc.AssetManager.Bundle = null;
    _lobbyScale: number = 0;
    _GameControl: loadGame = null;
    _spriteBg: cc.SpriteFrame = null;
    _loadHelper: gameHelper = null;


    pathGameBg: string = "Textures/bg";//游戏背景
    pathGameWorld: string = "Worlds/World1";//游戏地图皮肤
    pathGameSpeedUp: string = "speedUp";//加速技能皮肤
    pathGameSkillBt: string = "Skills/SkillBt";//Hero技能图标按钮
    pathGamePickup: string = "Skills/pickup";//带拾取的物品
    pathGameGun: string = "Skills/gunBody";//炮台预制体
    pathGameHSkillJson: string = "Skills/HSkill";//Hero技能配置文件路径
    pathGameTaskJson: string = "Task/task";//task配置文件路径
    pathGameRandomJson: string = "Skills/Skill";//Hero技能配置文件路径
    pathGameSkillBtBg: string = "Skills/skillBt";//技能按钮
    pathGameLoadBg: string = "Scenery/";//加载游戏场景背景资源
    pathGameMSkillJson: string = "Monster/MSkill";//怪兽技能参数配置文件
    pathGameMonster: string = "Monster";//怪兽皮肤配图集
    pathGameMonsterPfb: string = "Monster";//怪兽预制体
    pathParticlePfb: string = "Particle";//怪兽预制体
    pathGameWorldPfb: string = "Worlds";//跑道预制体路径
    pathGameStatuPfb: string = "Skills/Status";//持续状态预制体路径
    paathGameRefreshPfb: string = "Skills/Refresh";//增益状态预制体
    pathGameWorldJSonRoot: string = "Way/";//游戏地图配置目录
    pathGamestrong: string = "Skills/strong";//技能图标
    pathGameelevate: string = "Skills/elevate";//技能图标

    onLoad() {
        let self = this;
        this._loadHelper = gameHelper.getInstance();
        cc.assetManager.loadBundle('playLoadin', function (err, bundle) {
            if (err) {
                return console.error(err);
            }
            self._gameBundle = bundle;
            self.LoadGame();
        });
        this._barControl = this.barNode.getComponent(ProgressBar);
        //获取常驻节点所绑定的脚本 
        this._GameControl = cc.director.getScene().getChildByName('loadGame').getComponent(loadGame);
    }
    onDestroy() {
        this._spriteBg.decRef();
        this._spriteBg = null;
    }
    LoadGame() {
        cc.director.preloadScene("GameMan");
        this.loadBg(0.05);
        this.loadWorldAtlas(0.05);
        this.LoadSceneryFly(0.04);
        this.loadWorld(0.04)
        this.loadWayPrefab(0.04);
        this.loadStatusPrefab(0.04);
        this.loadRefreshPrefab(0.04);
        this.loadSpeedUpAtlas(0.05);
        this.loadSkillBtPfb(0.05);
        this.loadPickupPfb(0.05);
        this.loadSkillGunPfb(0.05);
        this.loadRandomJson(0.05);
        this.loadHeroPrefab(0.05);
        this.loadMonstersPfb(0.05);
        this.ParticlePrefab(0.05);
        this.loadMonstarJson(0.05);
        this.loadSkillBtBg(0.05);
        this.loadSkillelevate(0.05);
        this.loadSkillstrong(0.05);
        this.loadMonstersAtlas(0.05);
    }
    loadBg(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameBg, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                cc.log(err);
                return;
            }
            self._spriteBg = spriteFrame as cc.SpriteFrame;
            self.bgNode.getComponent(cc.Sprite).spriteFrame = self._spriteBg
            self._spriteBg.addRef();
            self.setProgressBar(scale);
        });
    }
    loadWorldAtlas(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameWorld, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._GameControl.WorldAtlas = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    loadSpeedUpAtlas(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameSpeedUp, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._GameControl.speedUpAtlas = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    loadSkillBtPfb(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameSkillBt, cc.Prefab, function (err, prefab) {
            if (err) {
                cc.log(err);
                return;
            }
            let _prefab = prefab as cc.Prefab;
            self._GameControl.SkillBtPrefab = _prefab;
            _prefab.addRef();
            self.setProgressBar(scale);
        });
    }
    loadPickupPfb(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGamePickup, cc.Prefab, function (err, prefab) {
            if (err) {
                cc.log(err);
                return;
            }
            let _prefab = prefab as cc.Prefab;
            self._GameControl.pickupPrefab = _prefab;
            _prefab.addRef();
            self.setProgressBar(scale);
        });
    }
    loadSkillGunPfb(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameGun, cc.Prefab, function (err, prefab) {
            if (err) {
                cc.log(err);
                return;
            }
            let _prefab = prefab as cc.Prefab;
            self._GameControl.SkillGunPrefab = _prefab;
            _prefab.addRef();
            self.setProgressBar(scale);
        });
    }
    loadRandomJson(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameRandomJson, function (err, jsonAsset) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = (jsonAsset as cc.JsonAsset).json;
            self._GameControl.RandomJson = details;
            self.setProgressBar(scale);
        });
    }
    loadSkillBtBg(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameSkillBtBg, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._GameControl.SkillBtBg = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    LoadSceneryFly(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameLoadBg + "fly01", cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._GameControl.SceneryFly = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    loadSkillelevate(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameelevate, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._GameControl.Skill_elevate = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    loadSkillstrong(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGamestrong, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._GameControl.Skill_strong = _atlas;
            _atlas.addRef();
            self.setProgressBar(scale);
        });
    }
    loadHeroPrefab(scale: number) {
        let self = this;
        this._gameBundle.load(this._loadHelper.getHeroPath(), cc.Prefab, function (err, prefab) {
            if (err) {
                cc.log(err);
                return;
            }
            let _prefab = prefab as cc.Prefab;
            self._GameControl.heroPrefab = _prefab;
            _prefab.addRef();
            self.setProgressBar(scale);
        });
    }
    loadMonstarJson(scale: number) {
        let self = this;
        this._gameBundle.load(this.pathGameMSkillJson, function (err, jsonAsset) {
            if (err) {
                cc.log(err);
                return;
            }
            let details = (jsonAsset as cc.JsonAsset).json;
            self._GameControl.MonsterJson = details;
            self.setProgressBar(scale);
        });
    }
    loadMonstersAtlas(scale: number) {
        let self = this;
        this._gameBundle.loadDir(this.pathGameMonster, cc.SpriteAtlas, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            self._GameControl.MonsterAtlas = new Map<string, cc.SpriteAtlas>();
            assets.forEach(element => {
                element.addRef();
                self._GameControl.MonsterAtlas.set(element.name, element as cc.SpriteAtlas);
            });
            self.setProgressBar(scale);
        });
    }
    loadMonstersPfb(scale: number) {
        let self = this;
        this._gameBundle.loadDir(this.pathGameMonsterPfb, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            self._GameControl.MonsterPrefab = new Map<string, cc.Prefab>();
            assets.forEach(element => {
                element.addRef();
                self._GameControl.MonsterPrefab.set(element.name, element as cc.Prefab);
            });
            self.setProgressBar(scale);
        });
    }
    ParticlePrefab(scale: number) {
        let self = this;
        this._gameBundle.loadDir(this.pathParticlePfb, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            self._GameControl.ParticlePrefab = new Map<string, cc.Prefab>();
            assets.forEach(element => {
                element.addRef();
                self._GameControl.ParticlePrefab.set(element.name, element as cc.Prefab);
            });
            self.setProgressBar(scale);
        });
    }
    loadWayPrefab(scale: number) {
        let self = this;
        this._gameBundle.loadDir(this.pathGameWorldPfb, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            self._GameControl.propsPrefab = new Map<string, cc.Prefab>();
            assets.forEach(element => {
                element.addRef();
                if (element.name == "flyWay") {
                    self._GameControl.flyWayPrefab = element as cc.Prefab;
                }
                else if (element.name == "runWay") {
                    self._GameControl.runWayPrefab = element as cc.Prefab;
                }
                else if (element.name == "wallWay") {
                    self._GameControl.wallWayPrefab = element as cc.Prefab;
                }
                else if (element.name == 'placeItem') {
                    self._GameControl.placeWayPrefab = element as cc.Prefab;
                }
                else {
                    self._GameControl.propsPrefab.set(element.name, element as cc.Prefab);
                }
            });
            self.setProgressBar(scale);
        });
    }
    loadStatusPrefab(scale: number) {
        let self = this;
        this._gameBundle.loadDir(this.pathGameStatuPfb, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            self._GameControl.statusPrefabs = new Map<string, cc.Prefab>();
            assets.forEach(element => {
                element.addRef();
                self._GameControl.statusPrefabs.set(element.name, element as cc.Prefab);
            });
            self.setProgressBar(scale);
        });
    }
    loadRefreshPrefab(scale: number) {
        let self = this;
        this._gameBundle.loadDir(this.paathGameRefreshPfb, cc.Prefab, function (err, assets) {
            if (err) {
                cc.log(err);
                return;
            }
            self._GameControl.refreshPrefabs = new Map<string, cc.Prefab>();
            assets.forEach(element => {
                element.addRef();
                self._GameControl.refreshPrefabs.set(element.name, element as cc.Prefab);
            });
            self.setProgressBar(scale);
        });
    }
    loadWorld(scale: number): any {
        let gameJson = gameHelper.getInstance().getLevelParam();
        let path = this.pathGameWorldJSonRoot + gameJson.World
        let self = this;
        this._gameBundle.load(path, function (err, jsonAsset) {
            if (!jsonAsset || err) {
                cc.log(err);
                return;
            }
            let param = (jsonAsset as cc.JsonAsset).json;
            self._GameControl.worldParam = param;
            self.loadScenery(self.pathGameLoadBg + param.Scenery);
            self.setProgressBar(scale);
        });
    }
    loadScenery(path: string) {
        let self = this;
        this._gameBundle.load(path, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            let _atlas = atlas as cc.SpriteAtlas;
            self._GameControl.SceneryBg = _atlas;
            _atlas.addRef();

            self.setProgressBar(0.06);
        });
    }
    setProgressBar(scale: number) {
        this._lobbyScale = parseFloat((this._lobbyScale + scale).toFixed(3));
        this._barControl.setScale(this._lobbyScale);
        if (this._lobbyScale >= 1) {
            cc.director.loadScene("GameMan");
        }
    }
}
